//
//  CollectClass.m
//  FunRead
//
//  Created by dllo on 15/5/26.
//  Copyright (c) 2015年 华东伟. All rights reserved.
//

#import "CollectClass.h"
#import <UIKit/UIKit.h>
@implementation CollectClass

- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

+ (NSArray *)deleteArray:(NSInteger)index
{
    NSString *path = [self path];
    NSMutableArray *arr = [self readArray];
    [arr removeObjectAtIndex:index];
    NSLog(@"%@", path);
    [arr writeToFile:path atomically:YES];
    
    return arr;
}
+ (void)writeArray:(NSDictionary *)newDic
{
    
    
    NSString *path = [self path];
    
    NSArray *arr = [self readArray];
    NSMutableArray *arr1 = [NSMutableArray arrayWithArray:arr];
    BOOL judge = [self judgeData:arr object:[NSString stringWithFormat:@"%@", [newDic objectForKey:@"bookid"] ]];
    if (judge) {
        // 换成通知框


        CollectClass *collcet = [[CollectClass alloc] init];
        [collcet showAlert:@"已收藏过"];
        
        
    }else {
        [arr1 addObject:newDic];
//        CollectClass *collect = [[CollectClass alloc]init];
//        [collect showAlert:@"收藏成功"];
    }
    [arr1 writeToFile:path atomically:YES];
//    NSLog(@"%@", arr1);
}


- (void)timerFireMethod:(NSTimer*)theTimer
{
    UIAlertView *promptAlert = (UIAlertView*)[theTimer userInfo]; [promptAlert dismissWithClickedButtonIndex:0 animated:NO]; promptAlert = NULL;
}
- (void)showAlert:(NSString *) _message
{
    //时间
    UIAlertView *promptAlert = [[UIAlertView alloc] initWithTitle:@"提示:" message:_message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
    [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(timerFireMethod:) userInfo:promptAlert repeats:YES]; [promptAlert show];
}

+ (BOOL)judgeData:(NSArray *)arr object:(NSString *)object
{
    BOOL judge = NO;
    for (NSMutableDictionary *dic in arr) {
        
        NSString *str = [NSString stringWithFormat:@"%@", [dic objectForKey:@"bookid"]];
                judge = [str isEqualToString:object];
        
        if (judge == YES) {
                    // 已经存在, 返回yes, 跳出forin循环
            return judge;
        //            break;
                }

        
       
    }
    return judge;
    
}
+ (void)fileExist
{
    NSFileManager *manager = [NSFileManager defaultManager];
    
    NSString *path = [self path];
    // 存在路径就一定存在文件, 至于文件里是否为空管不着
    
    BOOL judge = [manager fileExistsAtPath:path];
    if (judge) {
        // 存在, 没被意外删除
//        NSLog(@"文件存在");
    } else
    {
        // 不存在时
//        NSLog(@"文件bu存在");
        NSMutableArray *arr1 = [NSMutableArray array];
        NSString *path = [self path];
        [arr1 writeToFile:path atomically:YES];
        
    }
    
    
    
}

+ (NSMutableArray *)readArray
{
    [CollectClass fileExist];
    NSString *path = [self path];
    NSMutableArray *arr = [NSMutableArray arrayWithContentsOfFile:path];
    return  arr;
    
}

+ (NSString *)path
{
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *path = [array lastObject];
    
    path = [NSString stringWithFormat:@"%@/array1.txt", path];
    return path;
    //    NSLog(@"%@", path);
    
}



@end
